<template>
  <div class="item-one" ref="itemOne">
    <div class="title">
      <span>清算查询</span>
      <div>
        <el-button type="primary" :disabled="exxxx" @click="transactionexcel"><i class="el-icon-download"></i>导出</el-button>
        <el-button icon="el-icon-refresh"></el-button>
      </div>
    </div>
    <div class="container">
      <div class="screeningBox" >
        <div class="leftBox">
          <el-date-picker
            v-model="ctime"
            :clearable='false'
            type="daterange"
            align="center"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions">
          </el-date-picker>
          
        </div>
        <div class="rightBox">
          <div class="rightitem">
            <el-form ref="conditions" :model="conditions" label-width="80px">
            <el-input class="width60" v-model.trim="conditions.merchantNo" placeholder="请输入商户号"></el-input>
            <el-input class="width60" v-model.trim="conditions.merchantName" placeholder="请输入商户名称"></el-input>
            </el-form>
              <!-- <el-autocomplete
                class="inline-input"
                @keyup.enter.native="soso"
                v-model="conditions.shopno"
                :trigger-on-focus="false"
                :fetch-suggestions="querySearch"
                placeholder="请输入商户名称"
                @select="handleSelect"
              ></el-autocomplete> -->
          </div>
          <div class="rightitem">
          <el-button icon="el-icon-search" type="primary" @click="soso" :disabled="isDisable">搜索</el-button>
          <el-button icon="el-icon-refresh" type="primary" @click="reset">重置</el-button>
          </div>
        </div>
      </div>
      
     
      <div class="tableBox">
        <el-table
          v-loading="loading"
          ref="multipleTable"
          :data="merchantarr"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          stripe
          :header-cell-style="tableHeaderColor"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column align="center" type="index" label="序号"></el-table-column>
          <el-table-column prop="tradeDate" align="center" label="统计日期">
            <template slot-scope="scope">
              <span>{{scope.row.tradeDate | formatStata}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="交易金额(微信/支付宝/云闪付)">
            <template slot-scope="scope">
              <span>{{scope.row.wxTotalfee/100}}</span> /
              <span>{{scope.row.aliTotalfee/100}}</span> /
              <span>{{scope.row.uniTotalfee/100}}</span> 
            </template>
          </el-table-column>
          <el-table-column prop="merchantMccName" align="center" label="交易笔数(微信/支付宝/云闪付)">
            <template slot-scope="scope">
              <span>{{scope.row.wxTotalcount}}</span> /
              <span>{{scope.row.aliTotalcount}}</span> /
              <span>{{scope.row.uniTotalcount*1}}</span> 
            </template></el-table-column>
          <el-table-column prop="merchantMccName" align="center" label="手续费(微信/支付宝/云闪付)">
            <template slot-scope="scope">
              <span>{{scope.row.wxFee/100}}</span> /
              <span>{{scope.row.aliFee/100}}</span> /
              <span>{{scope.row.uniFee/100}}</span> 
            </template>
          </el-table-column>
          <el-table-column prop="" align="center" label="银行卡交易金额">
            <template slot-scope="scope">
              <span>{{scope.row.bankTotalfee/100}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="bankTotalcount" align="center" label="银行卡交易笔数"></el-table-column>
          <el-table-column prop="" align="center" label="银行卡手续费">
            <template slot-scope="scope">
              <span>{{scope.row.bankFee/100}}</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            @size-change="sizechange"
            :page-sizes="[10,20,30,40,50]"
            :page-size="cur_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total1"
          ></el-pagination>
        </div>
      </div>
      <br>
      <br>
    </div>
  </div>
</template>

<script>
import { getservice, getClearingQuery, getUserSelect,transactionexcel } from "@/api/api";
export default {
  name: "item-one",
  data: function() {
    return {
      loading: false,
      isDisable: false,
      exxxx: false,
      ctime:[],
      tableData: [],
      cur_page: 1,
      cur_size: 10,
      total1: 0,
      multipleSelection: [],
      restaurants: [],
      fid:'',
      visible: false,
      servicearr: [],
      merchantarr: [],
      combined: 10,
      pageCount: "",
      conditions: {
        servicelevel: "",
        servicename: "",
        merchantsstate: "",
        merchantsno: "",
        stardate: "",
        enddate: "",
        serviceid: "",
        aisle: "",
        shopno: "",
        merchantNo:'',
        merchantName:''
      },
      pickerOptions: {
        shortcuts: [
          {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', [date,date]);
            }
          },
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, 
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          },
          // {
          //   text: '最近三个月',
          //   onClick(picker) {
          //     const end = new Date();
          //     const start = new Date();
          //     start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
          //     picker.$emit('pick', [start, end]);
          //   }
          // }
        ]
      },
    };
  },
  filters:{
    formatStata(status) {
      var time_str= status;
		  var status = time_str.substr(0,10);
      return status;
    },
  },
  created() {
    const start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
    const year=start.getFullYear();
    const month=start.getMonth()+1;
    const date=start.getDate();
    if(month < 10){
      const date1 = year+'-'+ 0 +month+ '-'+date +' 00:00:00'
      const date2 = year+'-'+ 0 +month+ '-'+date +' 23:59:59'
      this.ctime =  [date1,date2]
    }else{
      const date1 = year+'-'+month+ '-'+date +' 00:00:00'
      const date2 = year+'-'+month+ '-'+date +' 23:59:59'
      this.ctime =  [date1,date2]
    }
    // this.merchantlist();
  },
  methods: {
     //修改thead 背景色
     tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background-color: #F0F0F0;font-weight: 500;font-size:14px;color:#000;'
      }
    },
    joininfo(val) {
      // console.log(val)
      this.$router.push({
        path: "/businessstatisticsinfo",
        query: {
          no: val
        }
      });
    },
    // 刷新
    refresh() {
      this.$router.replace({
        path: "/refresh",
        query: {
          t: Date.now()
        }
      });
    },
    // 分页导航
    handleCurrentChange(val) {
      this.cur_page = val;
      this.merchantlist();
      this.$nextTick(() => {
        document.querySelectorAll(".content")[0].scrollTop = "0";
      });
    },
    // 分页数量
    sizechange(val) {
      this.cur_size = val;
      this.merchantlist();

      this.$nextTick(() => {
        document.querySelectorAll(".content")[0].scrollTop = "0";
      });
    },
    //多选框
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //  重置
    reset() {
      this.conditions.shopno = "";
      this.conditions.serviceid = "";
      // this.ctime = [];
      this.fid = ''
      this.conditions.merchantNo = ''
      this.conditions.merchantName = ''
    },
    // 搜索
    soso() {
      // console.log(121123);
      // if (this.conditions.servicelevel == 1) {
      //   this.merchantlist("agent1Id");
      // } else if (this.conditions.servicelevel == 2) {
      //   this.merchantlist("agen2Id");
      // } else {
      //   this.merchantlist("agent1Id");
      // }
      this.merchantlist();
    },
    //商户模糊查询
    querySearch(queryString, cb) {
        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
        getUserSelect({ //商户模糊查询
          roleId: 5,
          keywords: this.conditions.shopno
        }).then(res => {
          // console.log(res.data)
          if(res.code == 200){
            this.restaurants = []
            for(var i=0;i<res.data.length;i++){
              this.restaurants.push({'value':res.data[i].fname,'id':res.data[i].fid})
            }
          }
        })
    },
   
    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    handleSelect(item) {
      console.log(item);
      this.fid = item.id
      // this.merchantlist(item.id) 
    },
    // 服务商级别
    servicelevel(val) {
      this.servicearr = [];
      getservice({
        type: val
      }).then(res => {
        console.log(res);
        for (var i = 0; i < res.data.rows.length; i++) {
          this.servicearr.push({
            value: res.data.rows[i].agentname,
            id: res.data.rows[i].id
          });
        }
      });
    },
    // 商户列表
    merchantlist(level) {
      // if(this.conditions.shopno !== ''){
      //   for(var i = 0; i < this.restaurants.length; i++){
      //     if(this.conditions.shopno == this.restaurants[i].value){
      //       this.fid  = this.restaurants[i].id
      //     }
      //   }
      // }
      this.loading = true
      this.isDisable = true
      getClearingQuery({
        page: this.cur_page,
        size: this.cur_size,
        startDate: this.ctime[0],
        endDate: this.ctime[1],
        merchantNo: this.conditions.merchantNo,
        merchantName: this.conditions.merchantName
      }).then(res => {
        // console.log(res);
        if (res.code == 405) {
          sessionStorage. removeItem("token");
          this.$router.push("/login");
        } else {
          console.log(res.data)
          if(res.code == 405){
            sessionStorage. removeItem('token')
            this.$router.push('/login');
          }else if(res.code == 200){
            this.isDisable = false
            this.loading = false
            if(res.data !== []){
              
              this.merchantarr = res.data;
              this.total1 = res.pageInfo.recordCount;
              this.pageCount = res.pageInfo.pageCount;
            }
          }else if(res.code == 500){
            this.isDisable = false
            this.loading = false
            this.$message.error(res.message);
          }
        }
      });
    },
    //导出
    transactionexcel(){
      const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        this.exxxx = true;
         transactionexcel({
          startDate: this.ctime[0],
          endDate: this.ctime[1],
          // merchantId: this.fid,
          merchantNo: this.conditions.merchantNo,
          merchantName: this.conditions.merchantName
         }).then(res => {
          if(res.code == 405){
              sessionStorage. removeItem('token')
              this.$router.push('/login');
              loading.close();
              this.exxxx = false;
            }else if(res.code == 200){
          let elink = document.createElement('a');
          elink.href = res.data;
          elink.click();
          loading.close();
          this.exxxx = false;
          }else{
            this.$message.error(res.message);
            loading.close();
            this.exxxx = false;
          }
        })
    }

  }
};
</script>
<style scoped lang='less'>
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;
}
.name {
  width: 25%;
}
.screeningBox {
  width: 100%;
  display: flex;
}
.leftBox,
.rightBox {
  width: 40%;
  padding: 0 35px;
  box-sizing: border-box;
}
.leftBox > .leftitem {
  margin-bottom: 15px;
}
.rightBox > .rightitem {
  margin-bottom: 15px;
}
.merchants {
  width: 50%;
}
.tableBox {
  margin-top: 20px;
}
.servicename {
  width: 60%;
}
.servicelevel {
  width: 20%;
}
.width20 {
  width: 20%;
}
.red {
  color: #f9607d;
}
.blue {
  color: #01aaef;
}
.stitle {
  padding: 5px 0;
  font-size: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  i {
    margin-left: 10px;
    margin-right: 5px;
  }
  .swarning {
    color: red;
  }
  .warning {
    color: #f9607d;
  }
}
.inline-input{
  width: 350px;;
}
.width60{
  width: 50%;
}
</style>